package com.yg.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yg.config.result.R;
import com.yg.config.result.ResultCode;
import com.yg.entity.readAndWriteTemplates.TeacherRead;
import com.yg.entity.singleEntity.Teacher;
import com.yg.service.TeacherService;
import com.yg.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    @Autowired
    private TeacherService teacherService;

    /**
     * 用户登录
     *
     * @param teacher
     * @return
     */
    @PostMapping("login")
    public R teacherLogin(@RequestBody Teacher teacher) {

        R flag = teacherService.teacherLogin(teacher);

        return flag;
    }

    /**
     * 强制修改密码
     *
     * @param teacherTemplate
     * @return
     */
    @PostMapping("postForcedTeacherPassowrd")
    public R putForcedTeacherPassowrd(
            @RequestBody TeacherRead teacherTemplate,
            HttpServletRequest request
    ) {

        Teacher one = null;
        try {
            String teacherId = teacherTemplate.getTeacherId();
            QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
            wrapper.eq("teacher_id", teacherId);
            one = teacherService.getOne(wrapper);
        } catch (Exception e) {
            e.printStackTrace();
            return R.ERROR().code(ResultCode.USERNOTEXIST).message("查询信息错误");
        }

        if (one != null) {
            one.setPassword(DigestUtils.md5DigestAsHex(DigestUtils.md5Digest(teacherTemplate.getPassword().getBytes())));
            teacherService.updateById(one);
            return R.OK().message("修改成功");
        } else {
            return R.ERROR().code(ResultCode.USERNOTEXIST).message("用户不存在");
        }

    }

    /**
     * 普通修改密码
     *
     * @param teacherTemplate
     * @param request
     * @return
     */
    @PostMapping("postTeacherPassword")
    public R putTeacherPassword(@RequestBody TeacherRead teacherTemplate, HttpServletRequest request) {
        String teacherId = JwtUtils.getTeacherIdByJwtToken(request);
        String oldPwd = teacherTemplate.getOldPwd();

        Teacher one = null;

        try {
            QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
            wrapper.eq("teacher_id", teacherId);
            one = teacherService.getOne(wrapper);
        } catch (Exception e) {
            e.printStackTrace();
            return R.ERROR().code(ResultCode.USERPASSWORDRESETERROR).message("查询信息错误");
        }

        if (one != null) {
            if (DigestUtils.md5DigestAsHex(DigestUtils.md5Digest(oldPwd.getBytes())).equals(one.getPassword())) {
                one.setPassword(DigestUtils.md5DigestAsHex(DigestUtils.md5Digest(teacherTemplate.getPassword().getBytes())));
                teacherService.updateById(one);
                return R.OK().message("修改成功");
            } else {
                return R.ERROR().code(ResultCode.USERPASSWORDRESETERROR).message("密码错误");
            }
        } else {
            return R.ERROR().code(ResultCode.USERNOTEXIST).message("无法查询到此教师");
        }
    }

    /**
     * 获得各个角色的数量
     *
     * @return
     */
    @GetMapping("getRoleNum")
    public R getRoleNum() {

//        R r = teacherService.getRoleNum();
        QueryWrapper<Teacher> teacherWrapper = new QueryWrapper<>();
        teacherWrapper.select("id");
        teacherWrapper.eq("vip", "v2");
        int teacherNum = teacherService.count(teacherWrapper);


        QueryWrapper<Teacher> adminWrapper = new QueryWrapper<>();
        adminWrapper.select("id");
        adminWrapper.eq("vip", "v1");
        int adminNum = teacherService.count(adminWrapper);

        return R.OK().data("teacherNum", teacherNum).data("adminNum", adminNum);
    }
}